﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using MicroFractal.iTunesBrowser.Business;

namespace MicroFractal.iTunesBrowser.Controls
{
    /// <summary>
    /// Interaction logic for Cover.xaml
    /// </summary>
    public partial class Cover : UserControl, ICloneable
    {

        #region Constructors

        public Cover(Album album)
        {
            InitializeComponent();
            this.MouseLeftButtonDown += new MouseButtonEventHandler(Cover_MouseLeftButtonDown);

            this.Album = album;

            lst.Width = this.Width;
            lst.Height = this.Height;
            lst.ItemsSource = this.Album.Songs;
            lst.Visibility =  Visibility.Hidden;
            Brush b = new SolidColorBrush(Color.FromArgb(120, Colors.White.R, Colors.White.G, Colors.White.B));
            lst.Background = b;

            this.canvasCover.Children.Add(lst);
        }

        ListView lst = new ListView();

        public Cover(Album album, string coverImage)
            : this(album)
        {
            if (!string.IsNullOrEmpty(coverImage) && System.IO.File.Exists(coverImage))
            {
                BitmapImage b = new BitmapImage();
                b.BeginInit();
                b.UriSource = new Uri(coverImage);
                b.EndInit();
                this.imgCover.Source = b;
            }
        }

        public Cover(Album album, System.IO.MemoryStream coverImage)
            : this(album)
        {
            if (coverImage != null && coverImage.Length > 0)
            {
                try
                {
                    BitmapImage b = new BitmapImage();
                    b.BeginInit();
                    b.StreamSource = coverImage;
                    b.EndInit();
                    this.imgCover.Source = b;
                }
                catch 
                {
                }
            }
        }

        #endregion

        #region Properties

        public string ArtistName { get; set; }
        
        public string AlbumName { get; set; }

        public Album Album { get; set; }

        public ImageSource CoverImage
        {
            get { return this.imgCover.Source; }
            set {
            if (value != null)
               {
                   this.imgCover.Source = value;
                    myBorder.BorderThickness = new Thickness(0);
                    canvasCover.Background.Opacity = 1;
                }
            else
                {
                    myBorder.BorderThickness = new Thickness(1);
                    canvasCover.Background.Opacity = .65;
                    //RadialGradientBrush r = new RadialGradientBrush(Colors.LightGray, Colors.Black);
                    //r.Center = new Point(120,120);
                    //r.Opacity = .65;
                    //canvasCover.Background = r;
                }
            }
        }

        #endregion

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
       
        }

        void Cover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Storyboard s = (Storyboard)this.canvasCover.Resources["UnFocusAnim"];
            s.Begin(this);
        }

    public new bool Equals (object obj)
    {
        if (obj == null || GetType() != obj.GetType()) 
        {
            return false;
        }

        Cover c = (Cover)obj;
        return c.ArtistName == this.ArtistName && c.AlbumName == this.AlbumName;
    }

    public void Flip()
    {
        if (lst.Visibility == Visibility.Hidden)
            lst.Visibility = Visibility.Visible;
        else
            lst.Visibility = Visibility.Hidden;
    }

        #region ICloneable Members

        public object Clone()
        {
            Cover c = new Cover(this.Album);
            c.imgCover.Source = this.imgCover.Source;
            c.canvasCover.Background.Opacity = this.canvasCover.Background.Opacity;
            c.myBorder.BorderThickness = this.myBorder.BorderThickness;
            return c;
        }

        #endregion
    }
}
